/*
 * HelloDataModelBroker.java
 *
 * Created on January 21, 2007, 9:06 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package myApp.myDataModel;
/*
 * Copyright 2007 microneering, Inc and James Gamber
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * BBDDemoApplication/myApp.myDataModel/HelloDataModelBroker.java
 */
import java.sql.SQLException;
import java.util.logging.Logger;

import bbd.BBDAPI;
import bbd.BBDAPIPrincipal;
import bbd.BBDBroker;
import bbd.BBDConnection;
import bbd.BBDRowArrayList;

/**
 * This Broker select1 and select2 methods get info from the API that is not
 * even based on any part of the database schema, but is information generated
 * in the stored procedure. This is an example of BBD principle of disconnecting
 * the application from the database schema.
 * 
 * @param R 
 * @param L 
 * @author james gamber
 */
public final class HelloDataModelBroker<R extends HelloDataModelRow, L extends BBDRowArrayList<R>>
		implements BBDBroker<R, L> { 

	static final Logger log = Logger.getLogger(HelloDataModelBroker.class
			.getName());

	/**
	 * Keys used to find the API SQL
	 */
	private final Class<R> rowClass = (Class<R>)HelloDataModelRow.class;
	
	 private final BBDAPI<R> selectHello = new BBDAPI<R>("test", "HelloWorld",rowClass);

	 private final BBDAPI<R> selectHello2 = new BBDAPI<R>("test", "HelloWorld2", rowClass);

	private final BBDAPI<R> selectHello3 = new BBDAPI<R>("test",
			"HelloDataModelBroker.selectHello3",
			"select 'Hello pass through sql!'", rowClass);
	
	/** Creates a new instance of HelloDataModelBroker */
	public HelloDataModelBroker() {
	}

	/**
	 * Create a list of R extends HelloDataModelRow.
	 * 
	 * @return Empty list.
	 */
	public L select(final R row) {

		return (L) new BBDRowArrayList<R>();
	}

	/**
	 * Ask the database for information using an BBD API that 
	 * has been deprecated.
	 * 
	 * @return Array list of type BBDArrayList<R> that contains info
	 * returned by database.
	 */
	public L select1() {

		final BBDAPIPrincipal userAccess = new BBDAPIPrincipal("Duke", "Java");
		selectHello.setBbdPrincipal(userAccess);
	
		final BBDConnection<R,L> myConnection = new BBDConnection<R,L>();
		L bbdRowAL = (L)new BBDRowArrayList<R>();

		try {
			bbdRowAL = myConnection
					.executeQuery(selectHello);

		} catch (final SQLException e) {
			e.printStackTrace();
			log.severe(e.toString());
		} 

		return bbdRowAL;
	}

	/**
	 * Ask the database for information by passing input parameter to 
	 * database.
	 * 
	 * @return Array list of type BBDArrayList<R> that contains info
	 * returned by database.
	 */
	public L select2() {

		final BBDAPIPrincipal userAccess = new BBDAPIPrincipal("Duke", "Java");
		selectHello2.setBbdPrincipal(userAccess);
		

		final BBDConnection<R,L> myConnection = new BBDConnection<R,L>();
		L bbdRowAL = (L)new BBDRowArrayList<R>();

		try {

			bbdRowAL = myConnection
					.executeQuery(selectHello2, "Duke");

		} catch (final SQLException e) {
			log.severe(e.toString());
		} 

		return bbdRowAL;
	}

	/**
	 * Ask the database for information using dynamic SQL and 
	 * bypassing the BBD API.
	 * 
	 * @return Array list of type BBDArrayList<R> that contains info
	 * returned by database.
	 */
	public L select3() {

		final BBDAPIPrincipal userAccess = new BBDAPIPrincipal("Duke", "Java");
		selectHello3.setBbdPrincipal(userAccess);

		final BBDConnection<R,L> myConnection = new BBDConnection<R,L>();
		BBDRowArrayList<R> bbdRowAL = new BBDRowArrayList<R>();

		try {

			bbdRowAL = myConnection.executeQuery(selectHello3);

		} catch (final SQLException e) {
			log.severe(e.toString());
		} 

		return (L) bbdRowAL;
	}

	public int insert(final R row) {
		return -1;
	}

	public int insert(final L list) {
		return -1;
	}

	public int update(final R row) {
		return -1;
	}

	public int update(final L list) {
		return -1;
	}

	public int delete(final R row) {
		return -1;
	}

	public int delete(final L list) {
		return -1;
	}

	public void setPrincipal(String name, String password) {
		// TODO Auto-generated method stub
		
	}

}
